package com.smart.security.security;

import java.util.Map;
import java.util.List;
import com.smart.util.BeanUtils;
import com.smart.util.SysUserDetails;
import com.smart.entity.SysMenu;
import com.smart.entity.SysUser;
import javax.annotation.Resource;
import com.smart.service.CarService;
import com.smart.service.MenuService;

import org.springframework.stereotype.Service;
import com.smart.common.exception.ApiException;
import com.smart.common.exception.SysExceptionEnum;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author lgb
 * @date 2020年6月15日 上午9:00:05 Copyright (C) 深圳工勘科技
 * @说明: 用户详情
 **/
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

	@Resource
	private CarService carService;
	@Resource
	private MenuService menuService;
	@Resource
	private com.smart.service.UserService userService;

	@Override
	public UserDetails loadUserByUsername(String un) throws UsernameNotFoundException {
		// 获取登录用户信息
		SysUser user = userService.getByUserName(un);
		if (BeanUtils.isNull(user)) {
			throw new ApiException(SysExceptionEnum.WRONG_USERNAME_OR_PASSWORD);
		}
		List<SysMenu> permissions = menuService.selectMenuList(user.getId());
		Map<String, Object> car = carService.getCarListByUserId(user.getId());
		if (car == null) {
			return new SysUserDetails(user, permissions, "");
		}
		return new SysUserDetails(user, permissions, car.get("car_no").toString());
	}

}